package com.example.database.demo.utils;

import org.apache.pdfbox.io.MemoryUsageSetting;
import org.apache.pdfbox.multipdf.PDFMergerUtility;

import java.io.File;
import java.io.IOException;
import java.util.List;

public class PdfFileUtil {

    public static File mergeFiles(List<String> pdfUrls, String targetFileName) throws IOException {
        PDFMergerUtility mergePdf = new PDFMergerUtility();
        for (String filePath : pdfUrls) {
            mergePdf.addSource(filePath);
        }
        // 设置合并生成pdf文件名称
        mergePdf.setDestinationFileName(targetFileName);
        // 合并pdf，使用默认内存设置
        mergePdf.mergeDocuments(MemoryUsageSetting.setupTempFileOnly());
        // 检查文件是否存在
        File mergedFile = new File(targetFileName);
        if (!mergedFile.exists()) {
            throw new IOException("合并后的文件不存在");
        }
        return mergedFile;

    }
}
